package com.bjwl.web.contrlller.branchstore;

import com.bjwl.pojo.*;
import com.bjwl.pojo.dto.StaffBrDto;
import com.bjwl.service.DailyService;
import com.bjwl.service.FindBaseService;
import com.bjwl.service.OrdersService;
import com.bjwl.service.StaffService;
import com.bjwl.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: czc
 * @Date:Create：in 2020/4/16 11:44
 * @Description:
 * @Modified By：
 * @Version: 1.0
 */
@Controller
@RequestMapping("/sta")
public class StaffController {

    @Autowired
    private StaffService staffService;

    @Autowired
    private DailyService dailyService;

    @Autowired
    private FindBaseService findBaseService;

   /* @RequestMapping("/jumpStaffAdd")
    private String jumpStaffAdd(Model model){

        return "branch_oa/staff-add";
    }*/

    //根据当前管理员的分店id查询员工信息
    @RequestMapping("/findStaffByBstoreid")
    public String findStaffByBstoreid(HttpSession httpSession, HttpServletRequest request,Integer bssign, Integer pageNum, Integer staffid, String staffvalue, String wstarttime, String wendtime) {
        if(pageNum == null){
            pageNum =1;
        }
        Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
        bssign = bsadmin1.getBsid();
        PageBean<Staff> staffList = staffService.findStaffByBstoreid(pageNum,bssign,staffid,staffvalue,wstarttime,wendtime);
        request.setAttribute("staffList", staffList);
        List<ServiceClass> serviceClassList = findBaseService.findAllServiceClass(bsadmin1.getBsid());
        httpSession.setAttribute("serclass",serviceClassList);
        StaffBrDto staffBrDto = new StaffBrDto(staffid,staffvalue,wstarttime,wendtime);
        request.setAttribute("staffBrDto",staffBrDto);
        return "branch_oa/staff-list";
    }

    //根据当前管理员的分店id查询员工信息
 /*  @RequestMapping("/findAllStaffById")
    public void findAllStaffById(HttpSession httpSession,Integer pageNum, Integer bssign,String staffname,Integer card) {
        if(pageNum == null){
            pageNum =1;
        }
        Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
        bssign = bsadmin1.getBsid();
        PageBean<Staff> staffListSettleStaff = staffService.findStaffBySettle(pageNum,bssign,staffname,card);
        httpSession.setAttribute("staffListSettleStaff", staffListSettleStaff);

    }*/


    //根据当前sid查询staff信息
    @RequestMapping("/updateShowStaff")
    public String updateShowStaff(HttpSession httpSession, Integer sid) {
        Bsadmin bsadmin = (Bsadmin)httpSession.getAttribute("bsadmin1");
        List<ServiceClass> serviceClassList = findBaseService.findAllServiceClass(bsadmin.getBsid());
        httpSession.setAttribute("serviceClassListaa",serviceClassList);
        Staff staff = staffService.updateShowStaff(sid);
        httpSession.setAttribute("staff", staff);
        return "branch_oa/staff-edit";
    }

    //根据sid修改当前信息
    @RequestMapping("/updateStaffByid")
    public @ResponseBody Boolean updateStaffByid(Staff staff) {
        staffService.updateStaffByid(staff);
        return true;
    }

    /**
     * 添加staff
     * @param staff
     */
    @RequestMapping("/addStaff")
    public @ResponseBody Boolean addStaff(@RequestBody Staff staff){
        Integer staff1 = staffService.findAllStaffByCard(staff.getCard());
        if(staff1 == null){
            if(staff.getSlogo().trim().equals("")){
                staff.setSlogo("/static/branch_oa/images/renshi.png");

            }
            staffService.addStaff(staff);
                return true;
        }else {
            return false;
        }

    }

    /**
     * 根据sid删除当前数据
     * @param sid
     */
    @RequestMapping("/deleteStaff")
    public @ResponseBody boolean deleteStaff(Integer sid){
        Staff staff = staffService.updateShowStaff(sid);

        staffService.deleteStaff(sid);
        return true;
    }

    /**
     * 查询当前分店下员工数
     * @param bssign
     * @return
     */
    public Integer staffCount(Integer bssign){
        Integer staffCount = staffService.staffCount( bssign);
        return staffCount;
    }

    //派单结算页面 选择员工
    @RequestMapping("/findStaffByBstoreidSettle")
    public String findStaffByBstoreidSettle(HttpSession httpSession, Integer bssign,Integer pageNum,Integer card,String staffname) {
        if(pageNum == null){
            pageNum =1;
        }
        Bsadmin bsadmin1 = (Bsadmin) httpSession.getAttribute("bsadmin1");
        bssign = bsadmin1.getBsid();
        PageBean<Staff> staffListss = staffService.findStaffByBstoreidSettle(pageNum,bssign,card,staffname);
        httpSession.setAttribute("staffListss", staffListss);
        return "branch_oa/slist";
    }

    //获取选择的员工
    @RequestMapping("/getStaffCard")
    public @ResponseBody Staff getStaffCard(@RequestBody String card , HttpSession httpsession){
        Bsadmin bsadmin = (Bsadmin) httpsession.getAttribute("bsadmin1");
        Integer bssign = bsadmin.getBsid();
        Staff staffSettle = staffService.finStaffByCardS(bssign,card);
        return staffSettle;
    }

}